// pages/column/sign/index.js
import regeneratorRuntime from '../../../utils/regeneratorRuntime.js'
import { data } from '../../../utils/data.js'
import util from '../../../utils/util.js'

const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    ...data.sginData,
    sginList: [],
    sginDay: 0,
    config: null,
    signSate: true,
    isSign: false,
    pupopRules: false,
    user: null,
    sign_tips: null,
    scrollIntoID: 'item0',
    scores: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getIndex()
  },

  async getIndex() {
    const year = await new Date().getFullYear()
    const month = await new Date().getMonth() + 1

    try {
      const list = await Promise.all([app.sgin.index(), app.sgin.calendar(year, month)])
      this.setData({
        total: list[0].total,
        config: list[0].config,
        signSate: list[0].has_sign,
        sign_tips: list[0].config.sign_tips,
        sign_log: list[0].sign_log.slice(0,10),
        sginDay: list[1].user.serial_sign,
        sginList: this.setSignDayList(list[1].user.serial_sign),
      })
      this._setSignDom()
    } catch(err) {
      app.toast(err)
    }
  },

  setSignDayList(countDays = 0) {
    let list = []
    let nums = 6
    if (countDays != 0) {
      for (let i = 0; i < countDays; i++) {
        list.push({
          day: i + 1,
          type: 1
        })
      }
    }
   
    for (let i = 0; i < nums; i++) {
      list.push({
        day: countDays + (i + 1),
        type: 0
      })
    }
    return list
  },

  onToSign() {
    if (!this.data.signSate) {
      app.sgin.submit().then(data => {
        if (data.state == 'success') {
          this.setData({
            signSate: true,
            scores: data.score,
            isSign: true
          })
          this.getIndex()
          setTimeout(() => {
            this.setData({ isSign: false })
          }, 2000)
        } else if (data.state == 'always_sign') {
          this.getIndex()
          app.toast('今日已签到');
        } else {
          this.getIndex()
          this.setData({ isSign: false })
        }
      })
        .catch((error) => {
          app.toast(error)
        })
    } else {
      wx.showToast({
        title: '您今天已经签到过了',
        icon: 'none'
      })
    }
  },

  onClose() { // 关闭打开的签到成功页面
    this.setData({ isSign: false })
  },

  _setSignDom() {
    let sginDay = this.data.sginDay
    this.setData({
      scrollIntoID: sginDay >= 6 ? `item${sginDay - 4}` : 'item0'
    })
  },

  pupopRewardClose() {
    this.setData({
      pupopRules: false
    })
  },

  onRules() {
    this.setData({
      pupopRules: true
    })
  },

  linkTap({ currentTarget: { dataset: { link } } }){
    wx.navigateTo({
      url: link,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this._setSignDom()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
   
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})